package co.baiku.boot.core.orm.dto;

import co.baiku.boot.core.orm.po.Po;
import co.baiku.boot.core.orm.po.Status;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

public class UserDetailDto extends Po<Long> implements UserDetails, Status {
    /**
     * 账号
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 用户状态:1正常,0禁用,2过期,3密码过期,4锁定
     */
    private Integer status = 1;

    /**
     * 最后一次重置密码时间
     */
    private LocalDateTime lastPasswordResetAt;
    /**
     * 角色
     */
    private String roleAlias;
    /**
     * 权限列表
     */
    private List<AuthDetailDto> roles;

    /**
     * [登录] 访问令牌
     */
    private String token;
    /**
     * [登录] 过期时间,剩余秒数
     */
    private Long expire;


    public UserDetailDto() {
    }

    public UserDetailDto(Long id, String username, List<AuthDetailDto> roles) {
        this.setId(id);
        this.username = username;
        this.roles = roles;
    }

    @Override
    public Integer getStatus() {
        return status;
    }

    @Override
    public void setStatus(Integer status) {
        this.status = status;
    }

    @Override
    public boolean isEnabled() {
        return status == 1;
    }

    @Override
    public boolean isAccountNonExpired() {
        return status != 2;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return status != 3;
    }

    @Override
    public boolean isAccountNonLocked() {
        return status != 4;
    }

    @Override
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public LocalDateTime getLastPasswordResetAt() {
        return lastPasswordResetAt;
    }

    public void setLastPasswordResetAt(LocalDateTime lastPasswordResetAt) {
        this.lastPasswordResetAt = lastPasswordResetAt;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return roles;
    }

    public List<AuthDetailDto> getRoles() {
        return roles;
    }

    public void setRoles(List<AuthDetailDto> roles) {
        this.roles = roles;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public Long getExpire() {
        return expire;
    }

    public void setExpire(Long expire) {
        this.expire = expire;
    }
}
